//
//  KTMartketTabView.swift
//  KOLToken
//
//  Created by XChen on 2020/3/12.
//  Copyright © 2020 iOS-team. All rights reserved.
//

import UIKit
import DynamicColor
import JXSegmentedView

class KTMartketTabView: UIView {
    
    var segmentedView : JXSegmentedView!
    var segmentedDataSource : JXSegmentedTitleDataSource!
    var listContainerView: JXSegmentedListContainerView!
        
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        buildUI()
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func buildUI() {
        segmentedView = JXSegmentedView()
        
        //1、配置数据源相关配置属性
        segmentedDataSource = JXSegmentedTitleDataSource()
        segmentedDataSource.titles = getTitles()
        segmentedDataSource.isTitleColorGradientEnabled = true
        segmentedDataSource.isItemSpacingAverageEnabled = false
        //2、关联dataSource
        segmentedView.dataSource = self.segmentedDataSource
        
        //3、配置指示器
        let indicator = JXSegmentedIndicatorLineView()
        indicator.indicatorWidth = 20
        segmentedView.indicators = [indicator]
        
        //4、配置JXSegmentedView的属性
        segmentedView.delegate = self
        addSubview(segmentedView)
        
        //5、初始化JXSegmentedListContainerView
        listContainerView = JXSegmentedListContainerView(dataSource: self)
        addSubview(listContainerView)
    //6、将listContainerView.scrollView和segmentedView.contentScrollView进行关联
        segmentedView.listContainer = listContainerView
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        segmentedView.frame = CGRect(x: 0, y: 0, width: bounds.size.width, height: 50)

        listContainerView.frame = CGRect(x: 0, y: 50, width: bounds.size.width, height: bounds.size.height - 50)
    }
    
    func getTitles() -> [String] {
        return ["资金", "分析", "简况"]
    }
    
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */
}

extension KTMartketTabView: JXSegmentedViewDelegate {
    func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
        if let dotDataSource = segmentedDataSource as? JXSegmentedDotDataSource {
            //先更新数据源的数据
            dotDataSource.dotStates[index] = false
            //再调用reloadItem(at: index)
            segmentedView.reloadItem(at: index)
        }
    }
}

extension KTMartketTabView: JXSegmentedListContainerViewDataSource {
    func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int {
        return segmentedDataSource.dataSource.count
    }

    func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate {
        if (0 == index) {
            return KTCoinFundsView()
        } else if (1 == index) {
            return KTCoinAnalysisView()
        }
        
        return KTCoinProfileView()
    }
}
